package com.biostime.qdingding.modules.qd.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.qdingding.common.service.CrudService;
import com.biostime.qdingding.modules.qd.dao.SigininDao;
import com.biostime.qdingding.modules.qd.entity.CourseOrder;
import com.biostime.qdingding.modules.qd.entity.Siginin;
import com.biostime.qdingding.modules.sys.entity.User;

/**
 * 签到Service
 * @author king
 * @version 2015-11-18
 */
@Service
@Transactional(readOnly = true)
public class SigininService extends CrudService<SigininDao, Siginin> {
	@Autowired
	private CourseOrderService courseOrderService;
	
	@Override
	public List<Siginin> findList(Siginin entity) {
		dataScopeFilter(entity, "dsf", "id=co.center_id", "id=a.create_by");
		
		return super.findList(entity);
	}

	@Transactional
	public Siginin signin(CourseOrder courseOrder, Siginin siginin) {
		courseOrder.setState("signed");
		courseOrderService.save(courseOrder);
		
		siginin.setStudentId(courseOrder.getStudentId());
		siginin.setCourseorderId(Integer.valueOf(courseOrder.getId()));
		siginin.setState(1);
		siginin.setSigntime(new Date());
		
		save(siginin);
		
		siginin = get(siginin.getId());
		
		return siginin;
	}
	
	@Transactional
	public Siginin absent(CourseOrder courseOrder, Siginin siginin) {
		courseOrder.setState("absented");
		courseOrderService.save(courseOrder);
		
		siginin.setStudentId(courseOrder.getStudentId());
		siginin.setCourseorderId(Integer.valueOf(courseOrder.getId()));
		siginin.setState(-1);
		siginin.setSigntime(new Date());
		
		save(siginin);
		
		siginin = get(siginin.getId());
		
		return siginin;
	}
	
	@Transactional
	public Siginin absent(CourseOrder courseOrder) {
		courseOrder.setState("absented");
		courseOrderService.save(courseOrder);
		
		Siginin siginin = new Siginin();
		
		siginin.setCreateBy(new User("0"));
		siginin.setUpdateBy(new User("0"));
		
		siginin.setStudentId(courseOrder.getStudentId());
		siginin.setCourseorderId(Integer.valueOf(courseOrder.getId()));
		siginin.setState(-1);
		siginin.setSigntime(new Date());
		
		save(siginin);
		
		return siginin;
	}

	public List<Siginin> findListByCourse(Siginin siginin) {
		return dao.findListByCourse(siginin);
	}

	public boolean exist(Siginin siginin) {
		// TODO Auto-generated method stub
		return dao.exist(siginin);
	}
}
